package top.lshaci.learning.springboot.quartz.config;

import cn.hutool.core.date.DateUtil;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.lshaci.learning.springboot.quartz.job.PrintJob;

import java.util.Date;

/**
 * QuartzConfig
 *
 * @author liangxu
 */
@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail redPrint() {
        return JobBuilder.newJob(PrintJob.class)
                .withIdentity("red", "print")
                .withDescription("usingJobData: color=red")
                .usingJobData("color", "red")
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger redPrintTrigger() {
        Date startDate = DateUtil.parse("2022-03-30 09:19:00", "yyyy-MM-dd HH:mm:ss").toJdkDate();
        return TriggerBuilder.newTrigger()
                .forJob(redPrint())
                .withIdentity("red", "print")
//                .startAt(startDate)
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                .withDescription("usingJobData: color=red")
                .build();
    }

    @Bean
    public JobDetail yellowPrint() {
        return JobBuilder.newJob(PrintJob.class)
                .withIdentity("yellow", "print")
                .withDescription("usingJobData: color=yellow")
                .usingJobData("color", "yellow")
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger yellowPrintTrigger() {
        Date startDate = DateUtil.parse("2022-03-30 09:17:00", "yyyy-MM-dd HH:mm:ss").toJdkDate();
        return TriggerBuilder.newTrigger()
                .forJob(yellowPrint())
                .withIdentity("yellow", "print")
                .startAt(startDate)
                .withSchedule(CronScheduleBuilder.cronSchedule("2/5 * * * * ?"))
                .withDescription("usingJobData: color=yellow")
                .build();
    }
}
